/*
 * Galaxium Messenger
 * 
 * Copyright (C) 2008 Philippe Durand <draekz@gmail.com>
 * 
 * License: GNU General Public License (GPL)
 * 
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

using System;
using System.IO;
using System.Net.Sockets;
using System.Text;
using System.Collections.Generic;

using Anculus.Core;

namespace Galaxium.Protocol.GaduGadu
{
	public class UserListEntry
	{
		public string FirstName;
		public string Surname;
		public string Nickname;
		public string AltNick;
		public string Mobile;
		public string Unknown1;
		public string Number;
		public string Email;
		public string UnknownNumber1;
		public string Unknown2;
		public string UnknownNumber2;
		public string Unknown3;
		public string UnknownNumber3;
		
		public override string ToString ()
		{
			StringBuilder sb = new StringBuilder ();
			
			sb.Append (FirstName); sb.Append (";");
			sb.Append (Surname); sb.Append (";");
			sb.Append (Nickname); sb.Append (";");
			sb.Append (AltNick); sb.Append (";");
			sb.Append (Mobile); sb.Append (";");
			sb.Append (Unknown1); sb.Append (";");
			sb.Append (Number); sb.Append (";");
			sb.Append (Email); sb.Append (";");
			sb.Append (UnknownNumber1); sb.Append (";");
			sb.Append (Unknown2); sb.Append (";");
			sb.Append (UnknownNumber2); sb.Append (";");
			sb.Append (Unknown3); sb.Append (";");
			sb.Append (UnknownNumber3); sb.Append (";");
			sb.Append ("\r\n");
			
			return sb.ToString ();
		}
	}
	
	public class UserListReplyCommand : GaduCommand
    {
        private byte _type;
     //   private bool _has_entry = false;
		private List<UserListEntry> _entries = new List<UserListEntry> ();
		
		public byte Type
        {
            get { return _type; }
        }
		
		public List<UserListEntry> Entries
		{
			get { return _entries; }
		}
		
		public UserListReplyCommand () : base (GaduConstants.CMD_IN_USERLIST_REPLY)
		{
			
		}
		
        protected override void Read (BinaryReader reader)
        {
        	_type = reader.ReadByte ();
        	
        	if (reader.BaseStream.Length > 1)
        	{
        		byte[] bytes = ReadString(reader);
				string userlist = Encoding.UTF8.GetString(bytes);
				
				Log.Debug ("GG UserList:\n"+userlist);
				
        		while (userlist.IndexOf ("\r\n") > -1)
				{
					UserListEntry entry = new UserListEntry ();
					string user = userlist.Substring (0, userlist.IndexOf ("\r\n"));
					userlist = userlist.Substring (userlist.IndexOf ("\r\n")+2, userlist.Length - (userlist.IndexOf("\r\n")+2));
					
					string[] contents = user.Split (';');
					
					entry.FirstName = contents[0];
					entry.Surname = contents[1];
					entry.Nickname = contents[2];
					entry.AltNick = contents[3];
					entry.Mobile = contents[4];
					entry.Unknown1 = contents[5];
					entry.Number = contents[6];
					entry.Email = contents[7];
					entry.UnknownNumber1 = contents[8];
					entry.Unknown2 = contents[9];
					entry.UnknownNumber2 = contents[10];
					entry.Unknown3 = contents[11];
					entry.UnknownNumber3 = contents[12];
					
					_entries.Add (entry);
				}
			}
        }
        
        public override string ToString ()
        {
        	return String.Empty;
        }
    }
}